<template>
  <div class="app-container">
    <main class="main-content">
      <div class="card">
        <div class="card-header">
          <h3>筛选条件</h3>
          <p>您可以在这里设置筛选条件以查看不同的报表数据。</p>
          <!-- 一个返回按钮的例子 -->
          <!-- <button class="btn btn-warning" @click="backToDashboard">返回仪表盘</button> -->
        </div>

        <div class="card-body">
          <div class="filter-container">
            <div class="form-row">
              <label>月份:</label>
              <select v-model="filters.month" class="form-control">
                <option v-for="month in monthOptions" :key="month" :value="month">{{ month }}</option>
              </select>
            </div>

            <div class="form-row" v-if="activeTab === 'inventory'">
              <label>仓库:</label>
              <select v-model="filters.warehouse" class="form-control">
                <option v-for="warehouse in warehouseOptions" :key="warehouse" :value="warehouse">{{ warehouse }}
                </option>
              </select>
            </div>

            <div class="form-row" v-if="activeTab === 'inventory'">
              <label>类别:</label>
              <select v-model="filters.category" class="form-control">
                <option v-for="category in categoryOptions" :key="category" :value="category">{{ category }}</option>
              </select>
            </div>
          </div>
        </div>
      </div>

      <!-- 选项卡导航 -->
      <div class="card">
        <div class="card-header">
          <h3>报表类型</h3>
        </div>
        <div class="card-body">
          <div class="tabs">
            <button :class="['tab-button', { active: activeTab === 'inventory' }]" @click="activeTab = 'inventory'">
              <font-awesome-icon icon="cubes" /> 库存报表
            </button>
            <button :class="['tab-button', { active: activeTab === 'production' }]" @click="activeTab = 'production'">
              <font-awesome-icon icon="chart-bar" /> 生产报表
            </button>
          </div>
        </div>
      </div>
      <!-- 库存报表内容 -->
      <div class="card" v-if="activeTab === 'inventory'">
        <div class="card-header">
          <h3>库存占比情况</h3>
        </div>
        <div class="card-body">
          <table>
            <thead>
              <tr>
                <th>物料名称</th>
                <th>类别</th>
                <th>仓库</th>
                <th>库存数量</th>
                <th>单位</th>
                <th>库存占比</th>
                <th>月度入库</th>
                <th>月度出库</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in filteredInventoryData" :key="item.id">
                <td>{{ item.material }}</td>
                <td>{{ item.category }}</td>
                <td>{{ item.warehouse }}</td>
                <td>{{ item.quantity.toLocaleString() }}</td>
                <td>{{ item.unit }}</td>
                <td>{{ item.ratio }}</td>
                <td>{{ item.inAmount.toLocaleString() }}</td>
                <td>{{ item.outAmount.toLocaleString() }}</td>
              </tr>
            </tbody>
          </table>

          <div v-if="filteredInventoryData.length === 0" class="empty-data">
            <div class="empty-placeholder">
              <font-awesome-icon icon="search" class="empty-icon" />
              <p>没有找到匹配的库存数据</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 生产报表内容 -->
      <div class="card" v-if="activeTab === 'production'">
        <div class="card-header">
          <h3>生产数据汇总</h3>
        </div>
        <div class="card-body">
          <table>
            <thead>
              <tr>
                <th>产品名称</th>
                <th>计划产量</th>
                <th>实际产量</th>
                <th>完成率</th>
                <th>计划时长(小时)</th>
                <th>实际时长(小时)</th>
                <th>效率比</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="item in filteredProductionData" :key="item.id">
                <td>{{ item.product }}</td>
                <td>{{ item.planQuantity }}</td>
                <td>{{ item.actualQuantity }}</td>
                <td>
                  <span :class="['badge', {
                    'badge-success': parseFloat(item.completionRate) >= 100,
                    'badge-warning': parseFloat(item.completionRate) >= 90 && parseFloat(item.completionRate) < 100,
                    'badge-danger': parseFloat(item.completionRate) < 90
                  }]">
                    {{ item.completionRate }}
                  </span>
                </td>
                <td>{{ item.plannedTime }}</td>
                <td>{{ item.actualTime }}</td>
                <td>{{ ((item.actualQuantity / item.planQuantity) * 100).toFixed(1) }}%</td>
              </tr>
            </tbody>
          </table>

          <div class="stats-container">
            <div class="stat-card">
              <h3>{{(filteredProductionData.reduce((sum, item) => sum + parseFloat(item.completionRate), 0) /
                filteredProductionData.length).toFixed(1)}}%</h3>
              <p>平均完成率</p>
            </div>
            <div class="stat-card">
              <h3>{{filteredProductionData.reduce((sum, item) => sum + item.actualQuantity, 0).toLocaleString()}}</h3>
              <p>总产量</p>
            </div>
            <div class="stat-card">
              <h3>{{filteredProductionData.reduce((sum, item) => sum + item.actualTime, 0)}}</h3>
              <p>总工时(小时)</p>
            </div>
          </div>

          <div v-if="filteredProductionData.length === 0" class="empty-data">
            <div class="empty-placeholder">
              <font-awesome-icon icon="search" class="empty-icon" />
              <p>没有找到匹配的生产数据</p>
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<!-- 报表分析页 -->
<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const activeTab = ref('inventory');

// 模拟库存数据
const inventoryData = reactive([
  { id: 1, material: '钢材', category: '原材料', warehouse: '原料仓', quantity: 5000, unit: '吨', ratio: '35%', month: '2023-08', inAmount: 2000, outAmount: 1500 },
  { id: 2, material: '铝材', category: '原材料', warehouse: '原料仓', quantity: 3000, unit: '吨', ratio: '21%', month: '2023-08', inAmount: 1200, outAmount: 800 },
  { id: 3, material: '轴承', category: '零部件', warehouse: '零件仓', quantity: 10000, unit: '个', ratio: '18%', month: '2023-08', inAmount: 5000, outAmount: 3000 },
  { id: 4, material: '螺丝', category: '零部件', warehouse: '零件仓', quantity: 500000, unit: '个', ratio: '12%', month: '2023-08', inAmount: 200000, outAmount: 150000 },
  { id: 5, material: '成品A', category: '成品', warehouse: '成品仓', quantity: 1000, unit: '台', ratio: '8%', month: '2023-08', inAmount: 500, outAmount: 300 },
  { id: 6, material: '成品B', category: '成品', warehouse: '成品仓', quantity: 800, unit: '台', ratio: '6%', month: '2023-08', inAmount: 400, outAmount: 200 }
]);

// 模拟生产数据
const productionData = reactive([
  { id: 1, product: '轴承套圈', planQuantity: 1000, actualQuantity: 950, completionRate: '95%', plannedTime: 120, actualTime: 115, month: '2023-08' },
  { id: 2, product: '齿轮组件', planQuantity: 500, actualQuantity: 480, completionRate: '96%', plannedTime: 80, actualTime: 78, month: '2023-08' },
  { id: 3, product: '液压阀体', planQuantity: 800, actualQuantity: 820, completionRate: '102.5%', plannedTime: 100, actualTime: 98, month: '2023-08' },
  { id: 4, product: '传动轴', planQuantity: 1200, actualQuantity: 1100, completionRate: '91.7%', plannedTime: 150, actualTime: 145, month: '2023-08' },
  { id: 5, product: '外壳铸件', planQuantity: 600, actualQuantity: 620, completionRate: '103.3%', plannedTime: 90, actualTime: 88, month: '2023-08' }
]);

// 筛选条件
const filters = reactive({
  month: '2023-08',
  warehouse: '全部',
  category: '全部'
});

// 计算筛选后的库存数据
const filteredInventoryData = computed(() => {
  return inventoryData.filter(item => {
    let match = true;
    if (filters.month !== '全部' && item.month !== filters.month) match = false;
    if (filters.warehouse !== '全部' && item.warehouse !== filters.warehouse) match = false;
    if (filters.category !== '全部' && item.category !== filters.category) match = false;
    return match;
  });
});

// 计算筛选后的生产数据
const filteredProductionData = computed(() => {
  return productionData.filter(item => {
    return filters.month === '全部' || item.month === filters.month;
  });
});

// 获取唯一的仓库和类别选项
const warehouseOptions = computed(() => {
  return ['全部', ...new Set(inventoryData.map(item => item.warehouse))];
});

const categoryOptions = computed(() => {
  return ['全部', ...new Set(inventoryData.map(item => item.category))];
});

// 获取月份选项
const monthOptions = computed(() => {
  const months = [...new Set([...inventoryData.map(item => item.month), ...productionData.map(item => item.month)])];
  return ['全部', ...months.sort()];
});

// 登出功能
const logout = () => {
  localStorage.removeItem('isLoggedIn');
  router.push('/login');
};
// 返回到仪表盘的函数（可选）
const backToDashboard = () => {
  router.push('/');
};
</script>


<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Microsoft YaHei', sans-serif;
}

body {
  background-color: #f5f7fa;
  color: #333;
  line-height: 1.6;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

/* 主界面样式 */
.app-container {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 250px;
  background-color: #1a1a1a;
  color: white;
  padding: 1.5rem 0;
}

.sidebar-header {
  padding: 0 1.5rem 1.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-menu {
  list-style: none;
  padding: 1rem 0;
}

.sidebar-menu li {
  padding: 0.75rem 1.5rem;
  cursor: pointer;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
}

.sidebar-menu li:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.sidebar-menu li.active {
  background-color: #2c80ff;
}

.sidebar-menu svg {
  margin-right: 0.75rem;
  width: 20px;
  text-align: center;
}

.main-content {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #dee2e6;
}

.header h2 {
  color: #1a1a1a;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-info img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 0.75rem;
}

.logout-link {
  cursor: pointer;
  color: #2c80ff;
  font-weight: 500;
}

.logout-link:hover {
  text-decoration: underline;
}

.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  margin-bottom: 1.5rem;
  overflow: hidden;
}

.card-header {
  padding: 1.25rem;
  border-bottom: 1px solid #dee2e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-body {
  padding: 1.25rem;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  text-align: center;
}

.stat-card h3 {
  font-size: 2rem;
  color: #2c80ff;
  margin-bottom: 0.5rem;
}

.stat-card p {
  color: #6c757d;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid #dee2e6;
}

th {
  background-color: #f8f9fa;
  font-weight: 600;
}

tr:hover {
  background-color: #f8f9fa;
}

.badge {
  padding: 0.35rem 0.65rem;
  border-radius: 50px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-success {
  background-color: rgba(52, 199, 89, 0.15);
  color: #34c759;
}

.badge-warning {
  background-color: rgba(255, 149, 0, 0.15);
  color: #ff9500;
}

.badge-danger {
  background-color: rgba(255, 59, 48, 0.15);
  color: #ff3b30;
}

.actions {
  display: flex;
  gap: 0.5rem;
}

.btn {
  display: inline-block;
  padding: 0.75rem 1.5rem;
  background-color: #2c80ff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  transition: background-color 0.3s;
  text-align: center;
  width: 100%;
}

.btn:hover {
  background-color: #1a6de0;
}

.btn-warning {
  background-color: #ff9500;
}

.btn-warning:hover {
  background-color: #e57c00;
}

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.875rem;
  width: auto;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  padding: 1.25rem;
  border-bottom: 1px solid #dee2e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-body {
  padding: 1.25rem;
}

.form-row {
  margin-bottom: 1rem;
}

.form-row label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.form-row input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #dee2e6;
  border-radius: 5px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-row input:focus {
  border-color: #2c80ff;
  outline: none;
}

.modal-footer {
  display: flex;
  gap: 10px;
  padding: 1.25rem;
  border-top: 1px solid #dee2e6;
  justify-content: flex-end;
}

.empty-data {
  text-align: center;
}

.empty-placeholder {
  padding: 3rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty-icon {
  font-size: 3rem;
  color: #dee2e6;
  margin-bottom: 1rem;
}

.empty-placeholder p {
  color: #6c757d;
  margin-bottom: 1.5rem;
}

.empty-placeholder .btn {
  width: auto;
}

@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    padding: 1rem 0;
  }

  .stats-container {
    grid-template-columns: 1fr;
  }
}

.tabs {
  display: flex;
  margin-bottom: 1rem;
}

.tab-button {
  padding: 0.75rem 1.5rem;
  background: none;
  border: none;
  cursor: pointer;
  font-weight: 500;
  color: #6c757d;
  border-bottom: 3px solid transparent;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.tab-button.active {
  color: #2c80ff;
  border-bottom-color: #2c80ff;
}

.tab-button:hover {
  color: #2c80ff;
}

.filter-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.form-row {
  display: flex;
  flex-direction: column;
}

.form-row label {
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.form-row select {
  padding: 0.5rem;
  border: 1px solid #dee2e6;
  border-radius: 4px;
}

.empty-data {
  text-align: center;
  padding: 3rem 1rem;
}

.empty-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty-icon {
  font-size: 3rem;
  color: #dee2e6;
  margin-bottom: 1rem;
}

.empty-placeholder p {
  color: #6c757d;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .filter-container {
    grid-template-columns: 1fr;
  }

  .tabs {
    flex-direction: column;
  }

  .tab-button {
    justify-content: center;
  }
}
</style>